<?php
require_once "controller/public/PublicSiteController.php";
require_once "dao/UserDAO.php";
require_once "model/User.php";
require_once "model/exception/UserException.php";

class LoginController extends PublicSiteController{

	function __construct(){
		$this->add_result("template_content", "public/login.tpl");
		parent::__construct();
	}


	public function initialize(){
		return $this->get_results();
	}

	public function login(){
		$email = $_REQUEST['email'];
		$password = sha1($_REQUEST['password']);
		
		$user;
		try{
			$user = UserDAO::get_user($email, $password);
			$_SESSION['user_id'] = $user->get_user_id();
			$_SESSION['user_name'] = $user->get_name();

			header("location: /?request=site.index.initialize");
		} catch(UserException $e){
			$this->add_result('error_msg', "Email o password incorrecto");
		}

	}

	public function logout(){
		session_destroy();
		header("location: /");
	}

}
?>
